package soundChaser;

import jniGlue.Librarylinker;
import FFT.FFTAnalyzer;

public class AudioAnalyzer extends Thread
{
	private double[][] autoArray = null;
	private double[][] fftArray = null;
	private double max = 0;
	private double max2 = 0;
	private int intesitaet = 0;
	private int intesitaet2 = 0;
	private double rot = 0;
	private double gruen = 0;
	private double blau = 0;
	private double rot2 = 0;
	private double gruen2 = 0;
	private double blau2 = 0;
	private Librarylinker fx5 = null;

	public AudioAnalyzer(Librarylinker fx5temp)
	{
		this.fx5 = fx5temp;
	}

	public void initialize(double[][] wavearray, long bitrate)
			throws InterruptedException
	{
		AutoCorrelation ac = new AutoCorrelation(wavearray[0].length,
				bitrate / 10, wavearray);
		ac.start();
		ac.join();
		autoArray = ac.ergebnisArray;
		// find the max of autocorrelation
		for (int i = 0; i < autoArray[0].length; i++)
		{
			if (max < autoArray[0][i])
			{
				max = autoArray[0][i];
			}
			if (max2 < autoArray[1][i])
			{
				max2 = autoArray[1][i];
			}
		}
	}

	public void analyze(int zeit, double[][] wavearray, long bitrate)
			throws InterruptedException
	{
		// schrittWeite is connected to max value 255
		rot = 0;
		gruen = 0;
		blau = 0;
		rot2 = 0;
		gruen2 = 0;
		blau2 = 0;

		FFTAnalyzer fft = new FFTAnalyzer(wavearray[0].length, bitrate,
				wavearray);
		fft.runThreads();
		fftArray = fft.ergebnisArray;

		for (int j = 0; j < 2; j++)
		{
			if (rot < fftArray[0][j])
			{
				rot = fftArray[0][j];
			}
			if (rot2 < fftArray[1][j])
			{
				rot2 = fftArray[1][j];
			}
		}
		for (int j = 2; j < 4; j++)
		{
			if (gruen < fftArray[0][j])
			{
				gruen = fftArray[0][j];
			}
			if (gruen2 < fftArray[1][j])
			{
				gruen2 = fftArray[1][j];
			}
		}
		for (int j = 4; j < 10; j++)
		{
			if (blau < fftArray[1][j])
			{
				blau = fftArray[1][j];
			}
			if (blau2 < fftArray[1][j])
			{
				blau2 = fftArray[1][j];
			}
		}

		// configure intesity
		intesitaet = (int) (255 * autoArray[0][zeit] / max);
		intesitaet2 = (int) (255 * autoArray[1][zeit] / max2);
		// configure color
		// light par

		//System.out.println(" rot " + rot + " blau " + blau + " gruen " + gruen);

		if (rot > 0.01)
		{
			fx5.senddmx(6, intesitaet);
			fx5.senddmx(0, 255);
		} else
		{
			fx5.senddmx(0, 0);
		}
		if (blau > 0.01)
		{
			
			fx5.senddmx(16, intesitaet2);
			fx5.senddmx(12, 255);
			
		
			
			/*Blau macht Farbwechsel
			if (intesitaet2 % 140 <= 15)
			{
				fx5.senddmx(16, intesitaet2);
				fx5.senddmx(12, 255);
				fx5.senddmx(13, 0);
			} else
			{
				fx5.senddmx(16, intesitaet2);
				fx5.senddmx(12, 0);
				fx5.senddmx(13, intesitaet2 % 140);
			}*/
		} else
		{
			fx5.senddmx(12, 0);
		}
		if (gruen > 0.01)
		{
			fx5.senddmx(52, intesitaet2);
		} else
		{
			fx5.senddmx(52, 0);
		}

		// light bars
		if (zeit % 10 == 0)
		{
			if (intesitaet == 0 && intesitaet2 == 0)
			{
				fx5.senddmx(100, 0);
			} else
			{
				fx5.senddmx(100, 60);
				if (intesitaet < intesitaet2)
				{
					intesitaet = intesitaet2;
				}

				if (rot > blau && rot > gruen)
				{
					// red
					fx5.senddmx(102, intesitaet);
					fx5.senddmx(105, intesitaet);
					fx5.senddmx(108, intesitaet);
					fx5.senddmx(104, 0);
					fx5.senddmx(107, 0);
					fx5.senddmx(110, 0);
					fx5.senddmx(103, 0);
					fx5.senddmx(106, 0);
					fx5.senddmx(109, 0);

				} else
				{
					if (blau > rot && blau > gruen)
					{
						// blue
						fx5.senddmx(104, intesitaet);
						fx5.senddmx(107, intesitaet);
						fx5.senddmx(110, intesitaet);
						fx5.senddmx(102, 0);
						fx5.senddmx(105, 0);
						fx5.senddmx(108, 0);
						fx5.senddmx(103, 0);
						fx5.senddmx(106, 0);
						fx5.senddmx(109, 0);

					} else
					{
						// green
						fx5.senddmx(103, intesitaet);
						fx5.senddmx(106, intesitaet);
						fx5.senddmx(109, intesitaet);
						fx5.senddmx(104, 0);
						fx5.senddmx(107, 0);
						fx5.senddmx(110, 0);
						fx5.senddmx(102, 0);
						fx5.senddmx(105, 0);
						fx5.senddmx(108, 0);
					}
				}
			}
		}
	}
}
